/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Size;
import com.kaaction.modules.mer.service.SizeService;

/**
 * 尺码Controller
 * @author huanghaiming
 * @version 2017-05-21
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/size")
public class SizeController extends BaseController {

	@Autowired
	private SizeService sizeService;
	
	@ModelAttribute
	public Size get(@RequestParam(required=false) String id) {
		Size entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sizeService.get(id);
		}
		if (entity == null){
			entity = new Size();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:size:view")
	@RequestMapping(value = {"list", ""})
	public String list(Size size, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Size> page = sizeService.findPage(new Page<Size>(request, response), size); 
		model.addAttribute("page", page);
		return "modules/mer/sizeList";
	}

	@RequiresPermissions("mer:size:view")
	@RequestMapping(value = "form")
	public String form(Size size, Model model) {
		model.addAttribute("size", size);
		return "modules/mer/sizeForm";
	}

	@RequiresPermissions("mer:size:edit")
	@RequestMapping(value = "save")
	public String save(Size size, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, size)){
			return form(size, model);
		}
		sizeService.save(size);
		addMessage(redirectAttributes, "保存尺码成功");
		return "redirect:"+Global.getAdminPath()+"/mer/size/?repage";
	}
	
	@RequiresPermissions("mer:size:edit")
	@RequestMapping(value = "delete")
	public String delete(Size size, RedirectAttributes redirectAttributes) {
		sizeService.delete(size);
		addMessage(redirectAttributes, "删除尺码成功");
		return "redirect:"+Global.getAdminPath()+"/mer/size/?repage";
	}

}